package com.forlove.app.splash;

import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.forlove.app.ForloveApplication;
import com.forlove.app.R;
import com.forlove.app.common.base.ui.BaseActivity;
import com.forlove.app.common.utils.PermissionsChecker;
import com.forlove.app.qselect.login.ui.ChoiceNewAgeActivity;

import butterknife.BindView;

/**
 * Created by yjl on 2017/9/27.
 * 引导页
 */

public class GuideActivity extends BaseActivity {
    @BindView(R.id.page_welcome)
    ViewPager mViewPager;
    @BindView(R.id.v_page_indecator)
    LinearLayout pageIndecator;

    @BindView(R.id.tx_go)
    TextView txGo;

    private long deltTime = 0;

    private static final int ImageList[] = {R.mipmap.ic_guide01,
            R.mipmap.ic_guide02, R.mipmap.ic_guide03,
            R.mipmap.ic_guide04, R.mipmap.ic_guide05};

    @Override
    public int getLayoutId() {
        return R.layout.activity_guide;
    }

    @Override
    public void initView() {
//        StatusBarCompat.compat(this);
        PermissionsChecker.getInstance().RequesetPermission(this);
        mViewPager.setAdapter(new MyAdapter());
        txGo.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(GuideActivity.this, ChoiceNewAgeActivity.class);
                toActivity(intent);
                GuideActivity.this.finish();
            }
        });
        showIndicatorView(0);
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int pos) {
                showIndicatorView(pos);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                // TODO Auto-generated method stub

            }
        });
    }


    private void showIndicatorView(int pos) {
        if (pos == pageIndecator.getChildCount() - 1) {
            pageIndecator.setVisibility(View.INVISIBLE);
            txGo.setVisibility(View.VISIBLE);
        } else {
            pageIndecator.setVisibility(View.VISIBLE);
            txGo.setVisibility(View.INVISIBLE);
        }
//        switch (pos) {
//            case 0:
//            case 2:
//                StatusBarCompat.compat(this, Color.parseColor("#2ac2b9"));
//                break;
//            case 1:
//            case 3:
//                StatusBarCompat.compat(this, Color.parseColor("#ff7785"));
//                break;
//        }
        for (int i = 0; i < pageIndecator.getChildCount(); i++) {
            ImageView child = (ImageView) pageIndecator.getChildAt(i);
            if (i == pos) {
                child.setBackgroundResource(R.drawable.guide_cicular_blue_corner);
            } else {
                child.setBackgroundResource(R.drawable.guide_cicular_gray_corner);
            }
        }
    }

    private class MyAdapter extends PagerAdapter {

        public MyAdapter() {

        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return ImageList.length;
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            // TODO Auto-generated method stub
            return arg0 == arg1;
        }

        @Override
        public void destroyItem(View view, int position, Object object) // 閿�姣両tem
        {
            ((ViewPager) view).removeView((View) object);
        }

        @Override
        public Object instantiateItem(View view, int position) // 瀹炰緥鍖朓tem
        {
            ImageView imageView = new ImageView(GuideActivity.this);
            imageView.setImageResource(ImageList[position]);
            Display display = getWindowManager().getDefaultDisplay();
            imageView.setLayoutParams(new ViewGroup.LayoutParams(display.getWidth(), display.getHeight()));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            ((ViewPager) view).addView(imageView, 0);
            if (position == ImageList.length - 1) {
                imageView.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        Intent intent = new Intent();
                        intent.setClass(GuideActivity.this, ChoiceNewAgeActivity.class);
                        toActivity(intent);
                        GuideActivity.this.finish();
                    }
                });
            }

            return imageView;
        }
    }

    @Override
    public void onBackPressed() {
        if (System.currentTimeMillis() - deltTime > 2000) {
            deltTime = System.currentTimeMillis();
            Toast.makeText(this, "再按一次退出应用", Toast.LENGTH_SHORT).show();
            return;
        }
        ForloveApplication.getInstance().exit();
    }
}
